<?php
include("model/product_place.php");
class products{
	function getProductList(){
		$productList = array();
		qdbconn();
		
		$query = "SELECT p.pro_id,p.pro_name,p.pro_description, b.br_name , c.cat_name,
				p.pro_price,    p.pro_issuedate,p.pro_status
					FROM products p, categories c,brands b 
					where c.cat_id = p.pro_cat_id 
					and p.pro_br_id=b.br_id  ";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$productList[$i]['pro_id']	= $row[0];
			$productList[$i]['pro_name']= $row[1];
			$productList[$i]['pro_description']= $row[2];
			$productList[$i]['br_name']= $row[3];
			$productList[$i]['cat_name']= $row[4];
			$productList[$i]['pro_price']= $row[5];
			$productList[$i]['pro_issuedate']= $row[6];
			$productList[$i]['pro_status']= $row[7];
			$product_place = new productplace();
			$productList[$i]['pro_placeList']= $product_place->getPlaceByProductId($row[0]);

			$i++;
		}
		return $productList;
	}
	
	function insertAProduct($data){
		qdbconn();
		$pro_name	 = $data['pro_name'];
		$pro_description = $data['pro_description'];
		$pro_price	 = $data['pro_price'];
		$pro_br_id	=$data['pro_br_id'];
		$pro_cat_id	=$data['pro_cat_id'];
		$pro_issuedate = $data['pro_issuedate'];
		
		
		$query = "SELECT count(1) FROM products
					WHERE pro_name = '$pro_name' " ;
		
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO products (pro_name,pro_description,pro_price,
				pro_issuedate,pro_br_id,pro_cat_id) 
						values ('$pro_name','$pro_description',$pro_price,'$pro_issuedate',
								$pro_br_id,$pro_cat_id)";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	function inactivateAProduct($pro_id){
		qdbconn();		
		$query = "UPDATE products SET pro_status = 0 where pro_id = $pro_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateAProduct($pro_id){
		qdbconn();		
		$query = "UPDATE products SET pro_status = 1 where pro_id = $pro_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editAProduct($data){
		qdbconn();
		$pro_id 			= $data['pro_id'];
		$pro_name 			= $data['pro_name'];
		$pro_description	= $data['pro_description'];
		$pro_price 			= $data['pro_price'];
		$pro_issuedate		= $data['pro_issuedate'];
		$pro_br_id			= $data['pro_br_id'];
		$pro_cat_id			= $data['pro_cat_id'];		
		
		
		$query = "UPDATE products SET pro_name='$pro_name', 
			pro_description='$pro_description',pro_issuedate='$pro_issuedate',
				pro_price=$pro_price,pro_br_id=$pro_br_id,pro_cat_id=$pro_cat_id
						 WHERE pro_id = $pro_id";
		
		$result = mysql_query($query);
		return $result;
	}
	
	function getProductsInfo($pro_id){
		qdbconn();		
		$query = "SELECT * FROM products
					WHERE pro_id = '$pro_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['pro_id'] 			= $row[0];
			$current_data['pro_name'] 			= $row[1];
			$current_data['pro_description'] 	= $row[2];
			$current_data['pro_br_id'] 			= $row[3];
			$current_data['pro_cat_id'] 		= $row[4];
			$current_data['pro_price'] 			= $row[5];
			$current_data['pro_issuedate'] 		= $row[6];
			$current_data['pro_status'] 		= $row[7];
			
		}
		return $current_data;
	
	}
}
?>